package com.fanjun.learning.multithread.Thread_Per_Message_Pattern.miniServer;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * Created by fanjun on 16/10/11.
 */
public class Service {
    public static void service(Socket socketClient) throws IOException {
        System.out.println(Thread.currentThread().getName() + ": Service.service( " + socketClient + ")begin");
        try {
            DataOutputStream out = new DataOutputStream(socketClient.getOutputStream());
            out.writeBytes("HTTP/1.0 200 OK\r\n");
            out.writeBytes("Content-type: text/html\r\n");
            out.writeBytes("\r\n");
            out.writeBytes("<html><head><title>CountDown</title></head><body>");
            out.writeBytes("<h1>Count down start!</h1>");
            for (int i = 10; i > 0; i--){
                System.out.println(Thread.currentThread().getName() + ": Countdown i = " + i);
                out.writeBytes("<h1>" + i + "</h1>");
                out.flush();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            out.writeBytes("</body></html>");
        } finally {
            socketClient.close();
        }
        System.out.println(Thread.currentThread().getName() + ": Service.service( " + socketClient + ")end");
    }
}
